/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BusquedaCobrador.java
 *
 * Created on 3/08/2011, 02:44:37 PM
 */

package co.srm.vistas.funcionales;

import co.srm.entidades.Cobrador;
import co.srm.entidades.Empresa;
import co.srm.manejadores.ManejadorCobrador;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author JORGE
 */
public class BusquedaCobrador extends javax.swing.JDialog {

    private  List <Cobrador> cobradorMemoria = null;
    /** Creates new form BusquedaCobrador */
    public BusquedaCobrador(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        inicializar();
    }


    public void inicializar(){
    llenarTablaCobrador();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaCobrador = new org.jdesktop.swingx.JXTable();
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tablaCobrador.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Identificacion", "Nombre", "Apellido", "Telefono", "Celular", "Direccion", "Empresa"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaCobrador.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaCobradorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaCobrador);

        jXPanel3.setBackground(new java.awt.Color(255, 224, 98));
        jXPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));

        jLabel13.setFont(new java.awt.Font("Arial", 3, 18)); // NOI18N
        jLabel13.setText("Busqueda Cobrador");

        javax.swing.GroupLayout jXPanel3Layout = new javax.swing.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel3Layout.createSequentialGroup()
                .addContainerGap(282, Short.MAX_VALUE)
                .addComponent(jLabel13)
                .addGap(279, 279, 279))
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaCobradorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaCobradorMouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==1){
            int fila = tablaCobrador.rowAtPoint(evt.getPoint());
            int columna = tablaCobrador.columnAtPoint(evt.getPoint());

            if((fila > -1) && (columna >-1)){
              columna = 0;
              String id = tablaCobrador.getValueAt(fila, columna).toString();
              Cobrador cobrador = null;

              try{
                cobrador = buscarCobrador(id);
              }catch(Exception ex){
                 Logger.getLogger(BusquedaCobrador.class.getName()).log(Level.SEVERE, null, ex);
                 ex.printStackTrace();
             }
              VentanaPersona.cobradorst = cobrador;
              enviarCobrador(cobrador);
              VentanaPersona.guardarPersona.setEnabled(false);
              VentanaPersona.botonModificar.setEnabled(true);
              this.setVisible(false);
            }else{
            JOptionPane.showMessageDialog(null, "Por favor seleccione un Cobrador");
            }
        }
    }//GEN-LAST:event_tablaCobradorMouseClicked

public void enviarCobrador(Cobrador c){
        VentanaPersona.campoIdentificacion.setText(c.getIdentificacion().toString());
        VentanaPersona.campoNombre.setText(c.getNombres());
        VentanaPersona.campoApellido.setText(c.getApellidos());
        VentanaPersona.campoTelefono.setText(c.getTelefono());
        VentanaPersona.campoCelular.setText(c.getCelular());
        VentanaPersona.comboEmpresa.setSelectedItem(c.getEmpresa());
        VentanaPersona.areaDireccion.setText(c.getDireccion());
}


    public void llenarTablaCobrador(){
    ManejadorCobrador mCo = new ManejadorCobrador();
    List<Cobrador> cobradores = mCo.todosLosCobradores();
    this.cobradorMemoria = cobradores;
    DefaultTableModel dtm = (DefaultTableModel) tablaCobrador.getModel();
        for (Cobrador cobrador : cobradores) {
            Object [] fila = { cobrador.getId(), cobrador.getIdentificacion(),cobrador.getNombres(),cobrador.getApellidos(), cobrador.getTelefono(), cobrador.getCelular(), cobrador.getDireccion(), cobrador.getEmpresa()};
            dtm.addRow(fila);
        }
    }


    public void limpiarTabla(){
        DefaultTableModel dtm = (DefaultTableModel) tablaCobrador.getModel();
        dtm.setRowCount(0);
     }

    public Cobrador buscarCobrador(String id){
    Long IdCobrador = null;
    Cobrador c = null;
   try{
    IdCobrador = Long.parseLong(id);
        }catch(Exception e){
        return null;
        }
        for (Cobrador cobrador : cobradorMemoria) {
            if(cobrador.getId().equals(IdCobrador)){
            c=cobrador;
            break;
            }
        }
      return c;
    }
    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                BusquedaCobrador dialog = new BusquedaCobrador(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel13;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private org.jdesktop.swingx.JXTable tablaCobrador;
    // End of variables declaration//GEN-END:variables

}
